<template>
  <div>
    <Echart :options="options"
            id="centreLeft1Chart"
            ref="centreLeft1Chart"
            height="220px"
            width="420px"></Echart>
  </div>
</template>

<script>
import Echart from '@/common/echart'
export default {
  data () {
    return {
      options: {},
      dataIndex: 0
    };
  },
  components: {
    Echart,
  },
  props: {
    cdata: {
      type: Object,
      default: () => ({})
    },
  },
  watch: {
    cdata: {
      handler (newData) {
        this.options = {
          color: [
            // "#37a2da",
            // "#32c5e9",
            // "#9fe6b8",
            // "#ffdb5c",
            // "#ff9f7f",
            // "#fb7293",
            // "#e7bcf3",
            // "#8378ea"
          ],
          tooltip: {
            trigger: "item",
            formatter: "{a} <br/>{b} : {c}人 ({d}%)"
          },
          toolbox: {
            show: true
          },
          calculable: true,
          legend: {
            orient: "horizontal",
            icon: "circle",
            bottom: 0,
            x: "center",
            data: newData.xData,
            textStyle: {
              color: "#fff"
            }
          },
          series: [
            {
              name: "统计",
              type: "pie",
              radius: [15, 45],
              roseType: "area",
              center: ["50%", "40%"],
              data: newData.seriesData,
              label: {
                normal: {
                  show: true,
                  fontSize:16,
                  formatter: '{b}{c}人  \n ({d}%)'
                }
              },
              emphasis: {
                label: {
                  show: true
                }
              }
            }
          ]
        }
      },
      immediate: true,
      deep: true
    }
  },
  mounted () {
    this.select()
  },
  methods: {
    // 开启定时器
    timemFun (chartdata) {
      this.timeInterval = setInterval(() => {
        chartdata.dispatchAction({
          type: 'downplay',
          seriesIndex: 0
        })
        this.cdata.seriesData.length == this.dataIndex + 1 ? (this.dataIndex = 0) : this.dataIndex++
        chartdata.dispatchAction({
          type: 'highlight',
          dataIndex: this.dataIndex
        })
      }, 2000);
    },
    select () {
      let chartdata = this.$refs.centreLeft1Chart.chart
      this.timemFun(chartdata)
      chartdata.on('mouseover', m => {
        clearInterval(this.timeInterval)
        chartdata.dispatchAction({
          type: 'downplay',
          seriesIndex: 0
        })
        chartdata.dispatchAction({
          type: 'highlight',
          dataIndex: m.dataIndex
        })
      })
      chartdata.on('mouseout', () => {
        this.timemFun(chartdata)
      })
    }
  }
};
</script>

<style lang="scss" scoped>
</style>